package com.beanu.arad.base.my_base;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.beanu.arad.R;
import com.beanu.arad.base.BaseModel;
import com.beanu.arad.base.BasePresenter;
import com.beanu.arad.base.ToolBarActivity;
import com.beanu.arad.widget.dialog.ProgressHUD;
import com.beanu.arad.widget.progressactivity.ProgressLayout;

import java.util.List;

/**
 * Created by Administrator on 2018/3/21.
 */
public abstract class MyBaseActivity<T extends BasePresenter, E extends BaseModel> extends ToolBarActivity<T, E> implements View.OnClickListener {
    protected Toolbar toolbar;
    protected TextView tv_title;//标题
    protected TextView tv_right_text;//标题栏右上角的文字
    protected ProgressLayout progressLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        Logg.e("onCreate..." + "savedInstanceState:" + savedInstanceState);
//        if (savedInstanceState != null) {
//            Arad.bus.post(10001);
//            Intent intent = new Intent(MyBaseActivity.this, MyBaseActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//            startActivity(intent);
//        }

        setContentView(getLayoutRes());
        initViews();
    }

    @Override
    public void setContentView(int layoutResID) {
        getDelegate().setContentView(layoutResID);

        setStatusBar();
    }

    protected void showLoadingView(List<Integer> skipIds) {//显示加载中...
        if (null != progressLayout) {
            if (isDataEmpty(skipIds)) {
                progressLayout.showLoading();
            } else {
                progressLayout.showLoading(skipIds);
            }
        }
    }

    protected void showContent(List<Integer> skipIds) {//显示页面正常的布局
        if (null != progressLayout) {
            if (isDataEmpty(skipIds)) {
                progressLayout.showContent();
            } else {
                progressLayout.showContent(skipIds);
            }
        }
    }

    protected void showEmptyView(List<Integer> skipIds) {//显示空数据页面
        if (null != progressLayout) {
            showEmptyView(0, getString(R.string.default_empty_title), getString(R.string.default_empty_detail), skipIds);
        }
    }

    protected void showEmptyView(int icon, String title, String description, List<Integer> skipIds) {
        if (icon == 0) {//没有传图标的资源id
            icon = R.mipmap.no_content;//默认空白图标
        }
        if (TextUtils.isEmpty(title)) {
            title = getString(R.string.default_empty_title);
        }
        if (TextUtils.isEmpty(description)) {
            title = getString(R.string.default_empty_detail);
        }

        if (isDataEmpty(skipIds)) {
            progressLayout.showEmpty(icon, title, description);
        } else {
            progressLayout.showEmpty(icon, title, description, skipIds);
        }
    }

    protected void showError(View.OnClickListener onClickListener) {//显示错误页面
        if (null != progressLayout) {
            showError(0, getString(R.string.default_error_title), getString(R.string.default_error_detail)
                    , getString(R.string.default_error_btn_txt), onClickListener);
        }
    }

    protected void showError(int icon, String title, String description, String btnTxt, View.OnClickListener onClickListener) {//显示错误页面
        if (icon == 0) {
            icon = R.mipmap.no_content;//默认错误图标
        }
        if (TextUtils.isEmpty(title)) {
            title = getString(R.string.default_error_title);
        }
        if (TextUtils.isEmpty(description)) {
            title = getString(R.string.default_error_detail);
        }
        if (TextUtils.isEmpty(btnTxt)) {
            title = getString(R.string.default_error_btn_txt);
        }

        progressLayout.showError(icon, title, description, btnTxt, onClickListener);
    }

    protected void initViews() {
        toolbar = findViewById(R.id.toolbar);
        tv_title = findViewById(R.id.tv_title);
        tv_right_text = findViewById(R.id.tv_right_text);
        mStatusBar = findViewById(R.id.arad_status_bar);

        progressLayout = findProgressLayout();

        if (null != toolbar) {
            toolbar.setNavigationIcon(R.mipmap.arad_ic_back_normal);
            toolbar.setNavigationOnClickListener(this);
            toolbar.setTitle("");
        }
        if (null != tv_right_text) {
            tv_right_text.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == -1) {//点击的是toolbar左上角的按钮
            onBackPressed();
        }
        if (v == tv_right_text) {//点击右上角的标题
            onRightTextClick();
        }
    }

    protected void onRightTextClick() {
    }

    protected void setHeadTitle(String title) {//设置标题栏的标题
        if (null != tv_title) {
            tv_title.setText(title);
        }
    }

    protected void setRightText(String rightText) {//设置标题栏右上角的文字
        if (null != tv_right_text) {
            tv_right_text.setText(rightText);
        }
    }

    protected String getRightText() {//获取标题栏右上角的文字
        if (null != tv_right_text) {
            return tv_right_text.getText().toString().trim();
        }
        return "";
    }

    protected void setNavigationIcon(int iconId) {
        if (null != toolbar) {
            toolbar.setNavigationIcon(iconId);
        }
    }

    protected void setNoNavigationIcon() {
        if (null != toolbar) {
            toolbar.setNavigationIcon(null);
        }
    }

    public boolean isDataEmpty(List list) {//判断list数据是否为空:null 或者size = 0 视为空
        return null == list || list.size() == 0;
    }

    protected void recycleBitmap(Bitmap bitmap) {//回收bitmap
        if (null != bitmap && !bitmap.isRecycled()) {
            bitmap.recycle();
        }
    }

    private ProgressHUD mProgressHUD;

    public void showProgress(boolean show) {//显示加载进度条
        try {
            showProgressWithText(show, "加载中...");
        } catch (Exception e) {

        }
    }

    public void showProgressWithText(boolean show, String message) {//显示加载进度条
        try {
            if (show) {
                //Context context, CharSequence message, boolean indeterminate, boolean cancelable,DialogInterface.OnCancelListener cancelListener
                mProgressHUD = ProgressHUD.show(this, message, true, true, null);
            } else {
                if (mProgressHUD != null) {
                    mProgressHUD.dismiss();
                }
            }
        } catch (Exception e) {

        }
    }

    //加载布局文件
    protected abstract int getLayoutRes();

    //加载空白(加载,错误)页面控件
    protected abstract ProgressLayout findProgressLayout();
}
